using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Masuit.Tools;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Erp;

/// <summary>
/// erp-销售退货表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v9")]
public class ErpSalesReturnService : IApplicationService 
{
    private readonly SugarRepository<ErpSalesReturn> _thisRepository;
    private readonly SugarRepository<ErpSalesReturnItem> _itemRepository;
    public ErpSalesReturnService(SugarRepository<ErpSalesReturn> thisRepository
    ,SugarRepository<ErpSalesReturnItem> itemRepository)
    {
        _thisRepository = thisRepository;
        _itemRepository = itemRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ErpSalesReturnDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Number.Contains(param.Key) || m.Reason.Contains(param.Key))
            .Includes(m => m.Order)
            .Includes(m=>m.Customer)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<ErpSalesReturnDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ErpSalesReturnDto> GetAsync(long id)
    {
        var model = await _thisRepository.AsQueryable()
            .Includes(m => m.Order)
            .Includes(m=>m.Customer)
            .FirstAsync(m=>m.Id==id);
        var res= model.Adapt<ErpSalesReturnDto>();
        var items = await _itemRepository.AsQueryable().Includes(m=>m.Product,m=>m.Unit).Where(m => m.ReturnId == id)
            .ToListAsync();
        res.List = items.Adapt<List<ErpSalesReturnItemDto>>();
        return res;
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task AddAsync(ErpSalesReturnDto model)
    {
        var toDay = await _thisRepository.CountAsync(m=>m.CreateTime.ToString("yyyy-MM")==DateTime.Now.ToString("yyyy-MM"));
        model.Number = "SAR".ResNumber(toDay, "yyyyMM");
        var returnId=await _thisRepository.InsertReturnSnowflakeIdAsync(model.Adapt<ErpSalesReturn>());
        foreach (var item in model.List)
        {
            item.ReturnId = returnId;
        }
        await _itemRepository.InsertRangeAsync(model.List.Adapt<List<ErpSalesReturnItem>>());
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task ModifyAsync(ErpSalesReturnDto model)
    {
        await _itemRepository.DeleteAsync(m => m.ReturnId==model.Id);
        await _thisRepository.UpdateAsync(model.Adapt<ErpSalesReturn>());
        foreach (var item in model.List)
        {
            item.ReturnId = model.Id;
        }
        await _itemRepository.InsertRangeAsync(model.List.Adapt<List<ErpSalesReturnItem>>());
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task DeleteAsync([FromBody] List<long> ids)
    {
        await _itemRepository.DeleteAsync(m => ids.Contains(m.ReturnId));
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
    }
}
